package com.crm.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.imageio.plugins.bmp.BMPImageWriteParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.crm.model.mdao.ContactMapper;
import com.crm.model.mdao.OrderInfoMapper;
import com.crm.model.mdao.OutgoodMapper;
import com.crm.model.mdao.OutgooddetailsMapper;
import com.crm.model.mdao.RefundgoodMapper;
import com.crm.model.mdao.RefunditemsMapper;
import com.crm.model.pojo.Backmoneyplan;
import com.crm.model.pojo.Customer;
import com.crm.model.pojo.Orderinfo;
import com.crm.services.BackmoneyplanService;
import com.crm.services.ContactServices;
import com.crm.services.CustomerServices;
import com.crm.services.OrderInfoServers;
import com.crm.services.OrderitemsServices;
import com.crm.services.ReturnMoneyService;
import com.crm.services.UserServices;
import com.crm.util.ListMapUtil;
import com.github.pagehelper.PageInfo;

@Controller
public class OrderController {

	@Autowired
	OrderInfoServers orderServer;
	@Autowired
	OrderInfoMapper orderInfoMapper;
	@Autowired
	CustomerServices customerServices;
	@Autowired
	OrderitemsServices orderitemsService;
	@Autowired
	ContactServices contactServer;
	@Autowired
	UserServices userServer;
	@Autowired
	BackmoneyplanService bpService;
	@Autowired
	ReturnMoneyService rmService;
	@Autowired
	OutgoodMapper ogMapper;
	@Autowired
	OutgooddetailsMapper ogdetailsMapper;
	@Autowired
	RefundgoodMapper refundgoodMapper;
	@Autowired
	RefunditemsMapper refunditemsMapper;

	@Autowired
	ContactMapper conMapper;

	/**
	 * 分页跳转到订单页
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@GetMapping("/sales/prosale/order")
	public ModelAndView order(Integer pageNum, Integer pageSize, Integer statusid, String search) {
		if (pageNum == null)
			pageNum = 1;
		if (pageSize == null)
			pageSize = 5;
		ModelAndView mv = new ModelAndView("sales/prosale/order");
		System.out.println("订单");
		// 分页查询订单
		List<Map<String, String>> findAllMaper = orderServer.selectByPager(pageNum, pageSize, statusid, search);
		// 获取发货单数据
		System.out.println(findAllMaper);
		String jsonString = JSONObject.toJSONString(findAllMaper);
		// 获取分页信息
		PageInfo<Map<String, String>> pager = new PageInfo<Map<String, String>>(findAllMaper);
		System.out.println(pager);
		String json = JSONObject.toJSONString(pager);
		System.out.println(jsonString);
		mv.addObject("list", jsonString);
		mv.addObject("page", pager);
		mv.addObject("pagejson", json);
		return mv;
	}

	/**
	 * 分页查询订单
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@PostMapping("/sales/prosale/order1")
	@ResponseBody
	public PageInfo<Map<String, String>> orderPage(Integer pageNum, Integer pageSize, Integer statusid, String search) {
		System.out.println("pageNum" + pageNum);
		System.out.println("pageSize" + pageSize);
		System.out.println("statusid" + statusid);
		System.out.println("search" + search);
		if (pageNum == null)
			pageNum = 1;
		if (pageSize == null)
			pageSize = 5;
		List<Map<String, String>> selectAll = orderServer.selectByPager(pageNum, pageSize, statusid, search);
		PageInfo<Map<String, String>> pager = new PageInfo<Map<String, String>>(selectAll);
		return pager;
	}

	/**
	 * 重定向到订单视图页
	 * 
	 * @return
	 * @return
	 * @return
	 */
	@GetMapping("/sales/prosale/orderView")
	public ModelAndView orderView(Integer id) {
		ModelAndView mv = new ModelAndView("/sales/prosale/orderViews");
		// 订单相关信息
		Map<String, String> selectAllView = orderServer.selectAllView(id);
		Map<String, String> orderinfo = orderServer.selectAllOrderByid(id);
		// 订单详情相关数据
		List<Map<String, String>> selectOrderItemView = orderitemsService.selectOrderItemView(id);
		// 发货单相关数据
		List<Map<String, String>> selectByoId = ogMapper.selectByoId(id);
		// 发货详情相关数据
		List<Map<String, String>> selectByOgid = null;
		System.out.println("发货单" + selectByoId);
		if (selectByoId.size() > 0) {
			selectByOgid = ogdetailsMapper
					.selectByOgid(Integer.parseInt(String.valueOf(selectByoId.get(0).get("ogId"))));
		}
		// 退货单数据
		List<ListMapUtil> list = new ArrayList<>();
		List<Map<String, String>> selectByoId2 = refundgoodMapper.selectByoId(id);
		for (Map<String, String> map : selectByoId2) {
			ListMapUtil listMapUtil = new ListMapUtil();
			listMapUtil.setMap(map);
			List<Map<String, String>> selectByRgid = refunditemsMapper
					.selectByRgid(Integer.parseInt(String.valueOf(map.get("rgId"))));
			listMapUtil.setListMap(selectByRgid);
			list.add(listMapUtil);
		}
		// 查询回款计划
		List<Map<String, String>> bmpMap = bpService.findByoId(id);
		List<Map<String, String>> rmMap = rmService.findByoId(id);
		System.out.println("发货详情" + selectByOgid);
		mv.addObject("orderView", selectAllView);
		String json = JSONObject.toJSONString(selectOrderItemView);
		mv.addObject("orderinfo", JSONObject.toJSONString(orderinfo));
		mv.addObject("bmpMap", JSONObject.toJSONString(bmpMap));
		mv.addObject("rmMap", JSONObject.toJSONString(rmMap));
		mv.addObject("orderItemView", json);
		mv.addObject("outgood", null);
		if (selectByoId.size() > 0)
			mv.addObject("outgood", selectByoId.get(0));// 发货单数据
		mv.addObject("outgoodCount", selectByoId.size());
		mv.addObject("outgoodItem", JSONObject.toJSONString(selectByOgid));// 发货详情数据
		System.out.println("退货信息" + list);
		mv.addObject("refunList", JSONObject.toJSONString(list));
		return mv;
	}

	/**
	 * 用于查询加载订单添加页的界面数据
	 * 
	 * @return
	 */
	@PostMapping("/sales/prosale/addOrderpage")
	@ResponseBody
	public JSONObject addOrderpage() {
		// 订单分类
		List<Map<String, String>> selectOrdertype = orderServer.selectOrdertype();
		// 支付方式分类
		List<Map<String, String>> selectOrderpay = orderServer.selectOrderpay();
		// 所有联系人信息
		List<Map<String, String>> selectContactAll = contactServer.selectAllBytype();
		List<Map<String, String>> selectUserAll = userServer.selectUserAll();
		System.out.println(selectContactAll);
		JSONObject jsonarray = new JSONObject();
		jsonarray.put("otype", selectOrdertype);
		jsonarray.put("opay", selectOrderpay);
		jsonarray.put("contact", selectContactAll);
		jsonarray.put("userAll", selectUserAll);
		return jsonarray;
	}

	/**
	 * 用于查询加载订单视图修改订单页的界面数据
	 * 
	 * @return
	 */
	@PostMapping("/sales/prosale/updateOrderpage")
	@ResponseBody
	public JSONObject updateOrderpage(@RequestParam("ID") Integer id) {
		// 用于加载客户信息
		List<Map<String, String>> selectAllCustomer = customerServices.selectAllCustomer();
		// 订单分类
		List<Map<String, String>> selectOrdertype = orderServer.selectOrdertype();
		// 发货分类
		List<Map<String, String>> selectFtype = orderServer.selectFtype();
		System.out.println(selectFtype);
		// 退货状态
		List<Map<String, String>> selectTtype = orderServer.selectTtype();
		System.out.println(selectTtype);
		// 支付方式分类
		List<Map<String, String>> selectOrderpay = orderServer.selectOrderpay();
		// 所有联系人信息
		List<Map<String, String>> selectContactAll = contactServer.selectAllBytype();
		// 所有者信息
		List<Map<String, String>> selectUserAll = userServer.selectUserAll();
		// 订单详情信息
		Map<String, String> selectAllOrderByid = orderServer.selectAllOrderByid(id);
		System.out.println(selectAllOrderByid);
		JSONObject jsonarray = new JSONObject();
		jsonarray.put("customer", selectAllCustomer);
		jsonarray.put("ttype", selectTtype);
		jsonarray.put("ftype", selectFtype);
		jsonarray.put("otype", selectOrdertype);
		jsonarray.put("opay", selectOrderpay);
		jsonarray.put("contact", selectContactAll);
		jsonarray.put("userAll", selectUserAll);
		jsonarray.put("orderInfo", selectAllOrderByid);
		return jsonarray;
	}

	/**
	 * 根据客户查询联系人
	 * 
	 * @return
	 */
	@PostMapping("/sales/prosale/seleOrdercust")
	@ResponseBody
	public JSONObject seleOrdercust(@RequestParam("ID") Integer id) {
		List<Map<String, String>> selectConbycustId = conMapper.selectConbycustId(id);
		JSONObject jsonarray = new JSONObject();
		jsonarray.put("cust", selectConbycustId);
		return jsonarray;
	}

	/**
	 * 添加订单
	 * 
	 * @param otitle
	 * @param otype
	 * @param osign
	 * @param osign1
	 * @param date1
	 * @param date2
	 * @param uid
	 * @param remark
	 * @param oconsignee
	 * @param conphone
	 * @param contaddress
	 * @param postcode
	 * @param orderpay
	 * @param customer
	 * @param radiosatus
	 * @return
	 */
	@PostMapping("/sales/prosale/addorder")
	@ResponseBody
	public String addOrder(@RequestParam(name = "otitle", required = false) String otitle,
			@RequestParam(name = "otype", required = false) Integer otype,
			@RequestParam(name = "osign", required = false) Integer osign,
			@RequestParam(name = "osign1", required = false) String osign1,
			@RequestParam(name = "date1", required = false) String date1,
			@RequestParam(name = "date2", required = false) String date2,
			@RequestParam(name = "uid", required = false) Integer uid,
			@RequestParam(name = "remark", required = false) String remark,
			@RequestParam(name = "oconsignee", required = false) String oconsignee,
			@RequestParam(name = "conphone", required = false) String conphone,
			@RequestParam(name = "contaddress", required = false) String contaddress,
			@RequestParam(name = "postcode", required = false) String postcode,
			@RequestParam(name = "orderpay", required = false) Integer orderpay,
			@RequestParam(name = "customer", required = false) String customer,
			@RequestParam(name = "radiosatus", required = false) Integer radiosatus) {
		Customer findByCustName = customerServices.findByCustName(customer);
		Orderinfo orderinfo = new Orderinfo();
		// orderinfo.setOId(1);//订单编号
		orderinfo.setCustomer(findByCustName);// 客户
		orderinfo.setOTitle(otitle);// 主题
		orderinfo.setOType(otype);// 订单分类
		orderinfo.setOPay(orderpay);// 付款方式
		orderinfo.setOData(java.sql.Date.valueOf(date1.substring(0, 10)));// 订单创建时间
		orderinfo.setOFadate(java.sql.Date.valueOf(date2));// 订单最晚发货时间
		orderinfo.setOSign(osign);// 客户签约人
		orderinfo.setUId(uid);// 订单所有人
		orderinfo.setOConsignee(oconsignee);// 收货人
		orderinfo.setoConphone(conphone);// 收货人电话
		orderinfo.setOAddress(contaddress);// 收货地址
		orderinfo.setOPostcode(postcode);// 邮编
		orderinfo.setORemark(remark);// 备注
		orderinfo.setODstatus(radiosatus);// 订单状态
		orderinfo.setOShipments(1093);// 发货状态
		orderinfo.setOSalesreturn(1096);
		orderinfo.setOStatus(1102);
		orderinfo.setOSummoney(new BigDecimal("10000"));
		System.out.println(orderinfo);
		Orderinfo addOrder = orderServer.addOrder(orderinfo);
		System.out.println("添加后数据：" + addOrder);
		return "123131";
	}

	/**
	 * 修改订单信息
	 * 
	 * @param id
	 * @param otitle
	 * @param otype
	 * @param osign
	 * @param osign1
	 * @param date1
	 * @param date2
	 * @param uid
	 * @param remark
	 * @param oconsignee
	 * @param conphone
	 * @param contaddress
	 * @param postcode
	 * @param orderpay
	 * @param customer
	 * @param radiosatus
	 * @param fire
	 * @return
	 */
	@PostMapping("/sales/prosale/updateorderif")
	@ResponseBody
	public String updateOrder(@RequestParam(name = "ID", required = false) Integer id,
			@RequestParam(name = "otitle", required = false) String otitle,
			@RequestParam(name = "otype", required = false) Integer otype,
			@RequestParam(name = "osign", required = false) Integer osign,
			@RequestParam(name = "osign1", required = false) String osign1,
			@RequestParam(name = "date1", required = false) String date1,
			@RequestParam(name = "date2", required = false) String date2,
			@RequestParam(name = "uid", required = false) Integer uid,
			@RequestParam(name = "textcommunication", required = false) String remark,
			@RequestParam(name = "oconsignee", required = false) String oconsignee,
			@RequestParam(name = "conphone", required = false) String conphone,
			@RequestParam(name = "contaddress", required = false) String contaddress,
			@RequestParam(name = "postcode", required = false) String postcode,
			@RequestParam(name = "orderpay", required = false) Integer orderpay,
			@RequestParam(name = "customer", required = false) Integer customer,
			@RequestParam(name = "radiosatus", required = false) Integer radiosatus,
			@RequestParam(name = "fire", required = false) String fire) {
		Customer findByCustName = customerServices.findByCustIdPage(customer);
		System.out.println("客户" + findByCustName);
		Orderinfo orderinfo = new Orderinfo();
		orderinfo.setOId(id);// 订单编号
		orderinfo.setCustomer(findByCustName);// 客户
		orderinfo.setOTitle(otitle);// 主题
		orderinfo.setOType(otype);// 订单分类
		orderinfo.setOPay(orderpay);// 付款方式
		orderinfo.setOData(java.sql.Date.valueOf(date1.substring(0, 10)));// 订单创建时间
		orderinfo.setOFadate(java.sql.Date.valueOf(date2));// 订单最晚发货时间
		orderinfo.setOSign(osign);// 客户签约人
		orderinfo.setUId(uid);// 订单所有人
		orderinfo.setOConsignee(oconsignee);// 收货人
		orderinfo.setoConphone(conphone);// 收货人电话
		orderinfo.setOAddress(contaddress);// 收货地址
		orderinfo.setOPostcode(postcode);// 邮编
		orderinfo.setORemark(remark);// 备注
		orderinfo.setODstatus(radiosatus);// 订单状态
		orderinfo.setOShipments(1093);// 发货状态
		orderinfo.setOSalesreturn(1096);
		orderinfo.setOStatus(1102);
		orderinfo.setOSummoney(new BigDecimal(fire));
		Orderinfo addOrder = orderServer.addOrder(orderinfo);
		System.out.println("添加后数据：" + addOrder);
		return "123131";
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@PostMapping("/sales/prosale/deleteorder")
	@ResponseBody
	public String dateleOrder(@RequestParam("ID") Integer id) {
		// 调用删除
		orderServer.deleteOrder(id);
		return "删除";
	}

	/**
	 * 订单审批
	 * 
	 * @param id
	 * @return
	 */
	@PostMapping("/sales/prosale/orderapprove")
	@ResponseBody
	public String orderapprove(@RequestParam("id") Integer id, @RequestParam("oid") Integer oid) {
		com.crm.model.mpojo.Orderinfo order = new com.crm.model.mpojo.Orderinfo();
		order.setoId(oid);
		order.setApprove(id);
		orderInfoMapper.updateByPrimaryKeySelective(order);
		return "订单审批";
	}

}
